package com.www.applyword.fragment;

import android.os.Bundle;
import android.view.ViewStub;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.BrokerAdapter;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.bean.HotJobBean;
import com.www.applyword.bean.RecordBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.SPUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2019/10/22 0022 16:32
 * version: 1.0
 */
public class BrokerFragment2 extends BaseFragment {
    @BindView(R.id.rl_broker)
    RecyclerView rlBroker;


    private BrokerAdapter mHotJobAdapter;
    private List<HotJobBean> hotJobBeans = new ArrayList<>();

    private int page = 1;
    private int Rows = 10;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    ViewStub vsEmpty;

    @Override
    protected int getLayoutResource() {
        return R.layout.broker_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        vsEmpty = rootView.findViewById(R.id.vs_empty);
        rlBroker.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlBroker.setItemAnimator(new DefaultItemAnimator());
        mHotJobAdapter = new BrokerAdapter(getActivity());
        rlBroker.setAdapter(mHotJobAdapter);

        refresh.setOnRefreshListener(refreshLayout -> initDate(true));

        refresh.setOnLoadMoreListener(refreshLayout ->initDate(false));
        initDate(true);
    }

    private void initDate(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");

        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("RecordType", "98");

        HttpClient.get(getActivity(), Constants.GETRECORDLIST, params, new CallBack<RecordBean>() {
            @Override
            public void onSuccess(RecordBean recordBean) {
                if (isRefresh) {
                    refresh.finishRefresh(true);
                    if (recordBean.getRecordList().size() == 0) {
                        showEmpty(true, vsEmpty, 0);
                    } else {
                        showEmpty(false, vsEmpty, 0);
                    }
                    mHotJobAdapter.replaceAll(recordBean.getRecordList(), true);
                } else {
                    if (recordBean.getRecordList().size() > 0) {
                        refresh.finishLoadMore(true);
                        mHotJobAdapter.addAll(recordBean.getRecordList());
                    } else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });
    }
}
